var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.frames["zwIframe"];
if (!upWindow) {
	upWindow = window.parent.frames["componentDiv"].frames["zwIframe"];
}
function fy_dialog_close() {
	// window.parent.$(".mask").remove();
	// window.parent.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
	window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger(
			"click");
}

function getUpFields(name) {
	return upWindow.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
	return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}

function addOrDelRepeat(name, newLength) {
	var fields = getUpFieldsByFieldval(name);
	var oldLength = fields.length;
	var tbody = getUpFieldsByFieldval(name).eq(0).parent();
	while (!tbody.is("tbody")) {
		tbody = tbody.parent();
	}
	for ( var i = 0, length = Math.abs(newLength - oldLength); i < length; i++) {
		if (newLength > oldLength) {
			upWindow.fy_addEmpty(tbody.children().last());
		} else {
			upWindow.fy_delCurrentRepeat(tbody.children().last());
		}
	}
	upWindow.fy_setReadonly();
}

function getUpFieldValue(field) {
	var fieldVal = field.attr("fieldVal");
	var editTag = field.hasClass(editClass);
	var browseTag = field.hasClass(browseClass);
	var addTag = field.hasClass(addClass);
	if (fieldVal != null && typeof (fieldVal) != 'undefined') {
		fieldVal = eval("(" + fieldVal + ")");
	} else {
		return null;
	}
	var obj = new Object();
	obj.displayName = fieldVal.displayName;
	obj.name = fieldVal.name;
	var tag = true;
	switch (fieldVal.inputType) {
	case "text":
	case "textarea":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name).val();
		} else if (browseTag) {
			obj.value = fieldVal.value;
		} else if (addTag) {
			obj.value = field.find("#" + fieldVal.name).val();
		}
		break;
	case "checkbox":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name).val();
		} else if (browseTag) {
			obj.value = field.find("#" + fieldVal.name).val();
		}
		break;
	case "radio":
		if (editTag) {
			obj.value = field.find(":radio:checked").attr("val4cal");
		} else if (browseTag) {
			obj.value = field.find(":radio:checked").attr("val4cal");
		}
		break;
	case "select":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name + " option:selected")
					.attr("val4cal");
		} else if (browseTag) {
			obj.value = field.find("#" + fieldVal.name).attr("val4cal");
		}
		break;
	case "date":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name).val();
			if (obj.value != "") {
				obj.value = obj.value + " 00:00";
			}
		} else if (browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "datetime":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name).val();
		} else if (browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "flowdealoption":
		if (editTag) {
			obj.value = fieldVal.value;
		} else if (browseTag) {
			obj.value = fieldVal.value;
		} else if (addTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "lable":
		if (editTag) {
			obj.value = fieldVal.value;
		} else if (browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "relationform":
		if (editTag) {
			obj.value = fieldVal.value;
		} else if (browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "relation":
		if (editTag || browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "project":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name).val();
		} else if (browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "member":
	case "multimember":
	case "account":
	case "multiaccount":
	case "department":
	case "multidepartment":
	case "post":
	case "multipost":
	case "level":
	case "multilevel":
		if (editTag) {
			obj.value = field.find("#" + fieldVal.name + "_txt").val();
			obj.id = field.find("#" + fieldVal.name).val();
		} else if (browseTag) {
			obj.value = field.find("#" + fieldVal.name).text();
		}
		break;
	case "attachment":
	case "image":
	case "document":
		if (editTag || browseTag) {
			var attNames = "";
			var tempField = field.find("#" + fieldVal.name);
			var tempFileId = "";
			if (tempField[0].tagName.toLowerCase() == "input") {
				tempFileId = tempField.val();
			} else if (tempField[0].tagName.toLowerCase() == "span") {
				tempFileId = tempField[0].innerHTML;
			}
			var atts = upWindow.getAttBySubreference(tempFileId);
			for ( var j = 0; j < atts.length; j++) {
				attNames += atts[j].filename + ",";
			}
			if (attNames != "") {
				attNames = attNames.substr(0, attNames.length - 1);
			}
			obj.value = attNames;
		}
		break;
	case "outwrite":
		if (editTag || browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "externalwrite-ahead":
		if (editTag || browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "exchangetask":
		if (editTag || browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	case "querytask":
		if (editTag || browseTag) {
			obj.value = fieldVal.value;
		}
		break;
	default:
		tag = false;
	}
	if (obj.value != undefined) {
		obj.value = obj.value.replace("\r\n", ";");
		obj.value = obj.value.replace("\n", ";");
	}
	return obj;
}
function setUpFieldValue(name, value) {
	upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input").val(
			value);
	upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
			.trigger("blur");
}

function setUpFieldValue2(field, value, displayValue, index) {
	var fieldVal = field.attr("fieldVal");
	var editTag = field.hasClass(editClass);
	var addTag = field.hasClass(addClass);
	value = trim(value);
	displayValue = trim(displayValue);
	if (fieldVal != null && typeof (fieldVal) != 'undefined') {
		fieldVal = eval("(" + fieldVal + ")");
	} else {
		return null;
	}

	var tag = true;
	switch (fieldVal.inputType) {
	case "text":
	case "textarea":
		if (editTag) {
			var inField = field.find("#" + fieldVal.name);
			inField.val(value);
			inField.trigger("blur");
		} else if (addTag) {
			var inField = field.find("#" + fieldVal.name);
			inField.val(value);
			inField.trigger("blur");
		}
		break;
	case "checkbox":
		if (editTag) {
			field.find("#" + fieldVal.name).attr("checked", true);
			field.find("#" + fieldVal.name).trigger("click");
		}
		break;
	case "radio":
		if (editTag) {
			field.find(":radio").eq(value).attr("checked", true);
			field.find(":radio").eq(value).trigger("click");
		}
		break;
	case "select":
		if (editTag) {
			var options = field.find("#" + fieldVal.name + " option");
			for ( var k = 0; k < options.length; k++) {
				if (options.eq(k).text() == value)
					options.eq(k).attr("selected", true);
			}
			field.find("#" + fieldVal.name).trigger("change");
		}
		break;
	case "date":
		if (editTag) {
			field.find("#" + fieldVal.name).val(value);
			field.find("#" + fieldVal.name).trigger("blur");
		}
		break;
	case "datetime":
		if (editTag) {
			field.find("#" + fieldVal.name).val(value);
			field.find("#" + fieldVal.name).trigger("blur");
		}
		break;
	case "member":
	case "multimember":
	case "account":
	case "multiaccount":
	case "department":
	case "multidepartment":
	case "post":
	case "multipost":
	case "level":
	case "multilevel":
		if (editTag) {
			field.find("#" + fieldVal.name).val(value);
		}
		break;
	}
}

/**
 * 去除左右空格
 */
function trim(str) {
	return (str) ? (typeof str == "string" ? str.replace(/^\s*/, "").replace(
			/\s*$/, "") : str) : "";
}
var accountValue = getUpFieldValue(getUpFieldsByFieldval("单位选择器"));
var accountName = accountValue.value;// 登录人员所在制造部名称
var accountId = accountValue.id;// 登录人员所在制造部ＩＤ

// ////////////////////树////////////////////////////
/**
 * 合格供应商
 */
var qualifiedSuppliersTree = null;// 合格供应商
/**
 * 潜在供应商
 */
var potentialSuppliersTree = null;// 潜在供应商

/**
 * 生成合格供应商供货范围树状图
 */
function createQualifiedSuppliersTree() {
	qualifiedSuppliersTree = new dhtmlXTreeObject("qualifiedSuppliersTreeDIV",
			"100%", "100%", 0);
	qualifiedSuppliersTree.setSkin("csh_dhx_skyblue");
	qualifiedSuppliersTree
			.setImagePath("./oa2uap/js/tree/imgs/csh_dhx_skyblue/");
	qualifiedSuppliersTree.enableCheckBoxes(1);
	qualifiedSuppliersTree.enableThreeStateCheckboxes(true);
	qualifiedSuppliersTree.loadXMLString(typeXML);
}
/**
 * 生成潜在供应商供货范围树状图
 */
function createPotentialSuppliersTree() {
	potentialSuppliersTree = new dhtmlXTreeObject("potentialSuppliersTreeDIV",
			"100%", "100%", 0);
	potentialSuppliersTree.setSkin("csh_dhx_skyblue");
	potentialSuppliersTree
			.setImagePath("./oa2uap/js/tree/imgs/csh_dhx_skyblue/");
	potentialSuppliersTree.enableCheckBoxes(1);
	potentialSuppliersTree.enableThreeStateCheckboxes(true);
	potentialSuppliersTree.loadXMLString(typeXML);
}

/**
 * 设置Tree的checkbox disabled
 */
function setTreeAllCheckBoxDisabled() {
	setCheckboxDisabled(qualifiedSuppliersTree, '0');
	setCheckboxDisabled(potentialSuppliersTree, '0');
	// qualifiedSuppliersTree.setCheck('0', true);
}
function setCheckboxDisabled(tree, id) {
	if (tree.hasChildren(id) > 0) {
		var itemArray = tree.getSubItems(id).split(',');
		for ( var i in itemArray) {
			tree.disableCheckbox(itemArray[i], 1);
			tree.setCheck(itemArray[i], false);
			setCheckboxDisabled(tree, itemArray[i]);
		}
	}
}

function setAllowCheckNodes(qualifiedSupplyRange, potentialSupplyRange) {
	for ( var i in qualifiedSupplyRange) {
		qualifiedSuppliersTree.disableCheckbox(qualifiedSupplyRange[i], 0);
	}

	for ( var i in potentialSupplyRange) {
		potentialSuppliersTree.disableCheckbox(potentialSupplyRange[i], 0);
	}

}

function setAllAllowCheckNodes() {
	setTreeAllCheckBoxDisabled();
	var qualifiedSupplyRangeNodes = document
			.getElementsByName("qualifiedSupplyRange");
	var potentialSupplyRangeNodes = document
			.getElementsByName("potentialSupplyRange");
	for ( var i = 0, size = qualifiedSupplyRangeNodes.length; i < size; i++) {
		setAllowCheckNodes(eval("("
				+ (qualifiedSupplyRangeNodes[i].value == "" ? "[]"
						: qualifiedSupplyRangeNodes[i].value) + ")"), eval("("
				+ (potentialSupplyRangeNodes[i].value == "" ? "[]"
						: potentialSupplyRangeNodes[i].value) + ")"));
	}
}

// //////////////////////////////////////////////
/**
 * 
 * @param code
 *            供应商编码
 * @param queryType
 *            查询类型 1:仅查询供应商名称 2: 查询供应商名称和供应商基本信息
 * @param operator
 *            处理方法
 */
function _ajax(url, operator) {
	setFieldsDisabled("company");
	document.getElementById("lodingDiv").style.display = '';
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	document.getElementById("lodingDiv").style.display = '';
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				text = xmlhttp.responseText;
				if (text == 2) {
					alert("用户还未登陆，或者已掉线");
				} else if (text == 3) {
					alert("供应商编码不能为空。");
				} else {
					operator(text);
				}
			} else {
				alert("连接服务器异常");
			}
			document.getElementById("lodingDiv").style.display = 'none';
			if (getUpFieldsByFieldval("总办填写表单_").length > 0) {
				setFieldsCanFill("company");
			}
		}
	};
	xmlhttp.open("get", url + "&temp=" + Math.random(), true);
	xmlhttp.send();
}
// /////////////////////start 重复表 增加删除行////////////////////////////
/**
 * 获取dom坐标
 * 
 * @param obj
 * @returns {___anonymous384_420}
 */
function getElemPos(obj) {
	var pos = {
		"top" : 0,
		"left" : 0
	};
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			pos.top += obj.offsetTop;
			pos.left += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		pos.left += obj.x;
	} else if (obj.x) {
		pos.top += obj.y;
	}
	return {
		x : pos.left,
		y : pos.top
	};
}

/**
 * 插入dom
 * 
 * @param newEl
 * @param targetEl
 */
function insertAfter(newEl, targetEl) {
	var parentEl = targetEl.parentNode;
	if (parentEl.lastChild == targetEl) {
		parentEl.appendChild(newEl);
	} else {
		parentEl.insertBefore(newEl, targetEl.nextSibling);
	}
}
/**
 * 清空dom的值
 * 
 * @param dom
 */
function clearValue(dom) {
	var childNodes = dom.childNodes;
	if (childNodes) {
		for ( var i = 0, length = childNodes.length; i < length; i++) {
			if (childNodes[i].tagName
					&& childNodes[i].tagName.toUpperCase() === 'INPUT') {
				childNodes[i].value = '';
			} else {
				clearValue(childNodes[i]);
			}
		}
	}
}
/**
 * 添加重复表的行
 * 
 * @param tr
 */
function addRepeatRow(tr) {
	var cloneTr = tr.cloneNode(true);
	clearValue(cloneTr);
	insertAfter(cloneTr, tr);
}
/**
 * 显示增加删除重复表的按钮
 * 
 * @param tr
 */
function showRepeatOperatorButton(tr) {
	event.cancelBubble = true;
	var rDiv = document.getElementById("repeatOperatorDiv");
	var pos = getElemPos(tr);
	rDiv.style.left = pos.x - 15;
	rDiv.style.top = pos.y;
	rDiv.style.display = '';
	document.getElementById("addImg").onclick = function() {
		event.cancelBubble = true;
		addRepeatRow(tr);
		rDiv.style.display = 'none';
	};
	document.getElementById("deleteImg").onclick = function() {
		event.cancelBubble = true;
		if (tr.parentNode.childNodes.length > 2) {
			tr.parentNode.removeChild(tr);
			setAllAllowCheckNodes();
		} else {
			alert("不能删除此行");
		}
		rDiv.style.display = 'none';
	};
}

function clearAllRepeatTable() {
	var tbody = document.getElementById("providerInfoTbody");
	while (tbody.childNodes.length > 2) {
		if (document.getElementsByName("organization").length == 1) {
			break;
		}
		tbody.removeChild(tbody.lastChild);
	}
	clearValue(tbody.childNodes[1]);
}

// /////////////////end 重复表 增加删除行/////////////////////////////

// /////////////////输入框样式设置 ////////////////////////////////////
/**
 * 设置输入框disabled
 */
function setFieldsDisabled(name, indexArray) {
	var setFieldDisabled = function(field) {
		field.disabled = true;
		if (field.type.toLowerCase() != 'radio') {
			field.className = 'disabledInput';
		}
	};
	var fields = document.getElementsByName(name);
	if (indexArray) {
		for ( var i in indexArray) {
			setFieldDisabled(fields[indexArray[i]]);
		}
	} else {
		for ( var i = 0, size = fields.length; i < size; i++) {
			setFieldDisabled(fields[i]);
		}
	}
}
/**
 * 设置输入框readOnly
 */
function setFieldsReadOnly(name, indexArray) {
	var setFieldReadOnly = function(field) {
		field.disabled = false;
		field.readOnly = true;
		if (field.type.toLowerCase() != 'radio') {
			field.className = 'readOnlyInput';
		}
	};
	var fields = document.getElementsByName(name);
	if (indexArray) {
		for ( var i in indexArray) {
			setFieldReadOnly(fields[indexArray[i]]);
		}
	} else {
		for ( var i = 0, size = fields.length; i < size; i++) {
			setFieldReadOnly(fields[i]);
		}
	}
}
/**
 * 设置输入框 可输入
 */
function setFieldsCanFill(name, indexArray) {
	var setFieldCanFill = function(field) {
		field.disabled = false;
		field.readOnly = false;
		if (field.type.toLowerCase() != 'radio') {
			field.className = 'manualInput';
		}
	};
	var fields = document.getElementsByName(name);
	if (indexArray) {
		for ( var i in indexArray) {
			setFieldCanFill(fields[indexArray[i]]);
		}
	} else {
		for ( var i = 0, size = fields.length; i < size; i++) {
			setFieldCanFill(fields[i]);
		}
	}
}

// //////////////////////////////////////////////////////////

// //////////////点击确定按钮/////////////////
/**
 * 获取TR的上级节点
 */
var getTRParentNode = function(node) {
	if (node && node.tagName && node.tagName.toUpperCase() != "TR") {
		return getTRParentNode(node.parentNode);
	} else {
		return node.parentNode;
	}
};

/**
 * 将数据写入到重复表
 * 
 * @param valueMap
 */
function writeToRepeatForm(valueMap) {
	var length = 0;
	var fieldName = null;
	for ( var name in valueMap) {
		length = valueMap[name].length;
		fieldName = name;
		break;
	}
	if (fieldName && length != 0) {
		// var nodes = up_doc.getElementsByName(fieldName);
		// var repeatLength = nodes.length;

		addOrDelRepeat(fieldName, length);
		for ( var name in valueMap) {
			// var form_nodes = up_doc.getElementsByName(name);
			var form_nodes = getUpFieldsByFieldval(name);
			for ( var i in valueMap[name]) {
				setUpFieldValue2(form_nodes.eq(i), valueMap[name][i]);
			}
		}
		// if (repeatLength != 0) {
		// var difference = repeatLength - length;
		// if (difference !== 0) {
		// var trParentNode = getTRParentNode(nodes[0]);
		//
		// if (difference > 0) {
		// while (difference != 0) {
		//
		// trParentNode.lastChild.id = 'oper';
		// window.dialogArguments.del();
		// trParentNode.lastChild.id = "";
		//
		// difference--;
		// }
		// } else {
		// while (difference != 0) {
		// var lastNode = trParentNode.lastChild;
		// lastNode.id = "oper";
		// window.dialogArguments.addEmpty();
		// lastNode.id = "";
		//
		// difference++;
		// }
		// }
		// }
		//
		// for (var name in valueMap) {
		// var form_nodes = up_doc.getElementsByName(name);
		// for (var i in valueMap[name]) {
		// form_nodes[i].value = valueMap[name][i];
		// }
		// }
		// } else {
		// alert("发生异常，为查找到表单中对应的输入框");
		// }

	}
	// else {
	// // alert("发生异常，将数据写入到重复表失败");
	// }
}

/**
 * 将失效的供应商信息写入表单
 */
function writeProviderInfoToForm() {
	var mapping = {
		organization : "所在组织",
		companyName : "失效公司名称",
		companyCode : "供应商编号",
		companyAddress : "失效地点",
		invalidDate : "失效日期",
		qualifiedSupplyRange : "供应商的合格供应商列表",
		potentialSupplyRange : "供应商的潜在供应商列表"
	};
	var valueMap = {
		"所在组织" : [],
		"失效公司名称" : [],
		"供应商编号" : [],
		"失效地点" : [],
		"失效日期" : [],
		"供应商的合格供应商列表" : [],
		"供应商的潜在供应商列表" : []
	};
	for ( var name in mapping) {
		var nodes = document.getElementsByName(name);
		for ( var i = 0, length = nodes.length; i < length; i++) {
			valueMap[mapping[name]].push(nodes[i].value);
		}
	}

	writeToRepeatForm(valueMap);

}
/**
 * 将需要进行失效审批的合格供货范围写入表单
 */
function writeQualifiedSupplyRangeToForm() {
	var valueMap = {
		"合格供应商_失效范围1" : [],
		"合格供应商_失效范围2" : [],
		"合格供应商_失效范围3" : [],
		"合格供应商_失效范围1PK" : [],
		"合格供应商_失效范围2PK" : [],
		"合格供应商_失效范围3PK" : []
	};
	var nodes = qualifiedSuppliersTree.getAllChecked() == "" ? []
			: qualifiedSuppliersTree.getAllChecked().split(',');
	for ( var i in nodes) {
		if (nodes[i] != 0) {
			if (getUpFieldValue(getUpFieldsByFieldval("单位选择器").eq(0)).id = "-8924145959034269856") {
				if (typeJSON[nodes[i]].binvgroupdot == "Y") {
					document.getElementsByName("groupManage")[0].checked = true;
					document.getElementsByName("groupManage")[1].checked = false;
				}
			}
			if (typeJSON[nodes[i]].presidentagreedto == "Y") {
				setUpFieldValue2(getUpFieldsByFieldval("是否需总裁审批").eq(0), 0);
				// up_doc.getElementsByName("是否需总裁审批")[0].checked = true;
				// up_doc.getElementsByName("是否需总裁审批")[up_doc
				// .getElementsByName("是否需总裁审批").length - 1].checked = false;
			}
			var parentId = qualifiedSuppliersTree.getParentId(nodes[i]);

			if (parentId != 0) {
				var pParentId = qualifiedSuppliersTree.getParentId(parentId);
				if (pParentId != 0) {
					valueMap["合格供应商_失效范围1"].push(qualifiedSuppliersTree
							.getItemText(pParentId));
					valueMap["合格供应商_失效范围1PK"].push(pParentId);
					valueMap["合格供应商_失效范围2"].push(qualifiedSuppliersTree
							.getItemText(parentId));
					valueMap["合格供应商_失效范围2PK"].push(parentId);
					valueMap["合格供应商_失效范围3"].push(qualifiedSuppliersTree
							.getItemText(nodes[i]));
					valueMap["合格供应商_失效范围3PK"].push(nodes[i]);
				} else {
					valueMap["合格供应商_失效范围1"].push(qualifiedSuppliersTree
							.getItemText(parentId));
					valueMap["合格供应商_失效范围1PK"].push(parentId);
					valueMap["合格供应商_失效范围2"].push(qualifiedSuppliersTree
							.getItemText(nodes[i]));
					valueMap["合格供应商_失效范围2PK"].push(nodes[i]);
					valueMap["合格供应商_失效范围3"].push("");
					valueMap["合格供应商_失效范围3PK"].push("");
				}
			} else {
				valueMap["合格供应商_失效范围1"].push(qualifiedSuppliersTree
						.getItemText(nodes[i]));
				valueMap["合格供应商_失效范围1PK"].push(nodes[i]);
				valueMap["合格供应商_失效范围2"].push("");
				valueMap["合格供应商_失效范围2PK"].push("");
				valueMap["合格供应商_失效范围3"].push("");
				valueMap["合格供应商_失效范围3PK"].push("");
			}
		} else {
			for ( var name in valueMap) {
				valueMap[name].push("");
			}
		}
	}
	writeToRepeatForm(valueMap);
}
/**
 * 将需要进行失效审批的潜在供货范围写入表单
 */
function writePotentialSupplyRangeToForm() {
	var valueMap = {
		"潜在供应商_失效范围1" : [],
		"潜在供应商_失效范围2" : [],
		"潜在供应商_失效范围3" : [],
		"潜在供应商_失效范围1PK" : [],
		"潜在供应商_失效范围2PK" : [],
		"潜在供应商_失效范围3PK" : []
	};
	var nodes = potentialSuppliersTree.getAllChecked() == "" ? []
			: potentialSuppliersTree.getAllChecked().split(',');
	for ( var i in nodes) {
		if (nodes[i] != 0) {
			if (getUpFieldValue(getUpFieldsByFieldval("单位选择器").eq(0)).id != "-8924145959034269856") {
				if (typeJSON[nodes[i]].binvgroupdot == "Y") {
					document.getElementsByName("groupManage")[0].checked = true;
					document.getElementsByName("groupManage")[1].checked = false;
				}
			}
			if (typeJSON[nodes[i]].presidentagreedto == "Y") {
				setUpFieldValue2(getUpFieldsByFieldval("是否需总裁审批").eq(0), 0);
			}
			var parentId = potentialSuppliersTree.getParentId(nodes[i]);

			if (parentId != 0) {
				var pParentId = potentialSuppliersTree.getParentId(parentId);
				if (pParentId != 0) {
					valueMap["潜在供应商_失效范围1"].push(potentialSuppliersTree
							.getItemText(pParentId));
					valueMap["潜在供应商_失效范围1PK"].push(pParentId);
					valueMap["潜在供应商_失效范围2"].push(potentialSuppliersTree
							.getItemText(parentId));
					valueMap["潜在供应商_失效范围2PK"].push(parentId);
					valueMap["潜在供应商_失效范围3"].push(potentialSuppliersTree
							.getItemText(nodes[i]));
					valueMap["潜在供应商_失效范围3PK"].push(nodes[i]);
				} else {
					valueMap["潜在供应商_失效范围1"].push(potentialSuppliersTree
							.getItemText(parentId));
					valueMap["潜在供应商_失效范围1PK"].push(parentId);
					valueMap["潜在供应商_失效范围2"].push(potentialSuppliersTree
							.getItemText(nodes[i]));
					valueMap["潜在供应商_失效范围2PK"].push(nodes[i]);
					valueMap["潜在供应商_失效范围3"].push("");
					valueMap["潜在供应商_失效范围3PK"].push("");
				}
			} else {
				valueMap["潜在供应商_失效范围1"].push(potentialSuppliersTree
						.getItemText(nodes[i]));
				valueMap["潜在供应商_失效范围1PK"].push(nodes[i]);
				valueMap["潜在供应商_失效范围2"].push("");
				valueMap["潜在供应商_失效范围2PK"].push("");
				valueMap["潜在供应商_失效范围3"].push("");
				valueMap["潜在供应商_失效范围3PK"].push("");
			}
		} else {
			for ( var name in valueMap) {
				valueMap[name].push("");
			}
		}
	}
	writeToRepeatForm(valueMap);
}

/**
 * 点击确定按钮
 */
function onButtonClick() {
	var companySelect = document.getElementsByName("company")[0];
	if (companySelect.value && companySelect.value != '') {

		var providerNodes = document.getElementsByName("companyName");
		var qualifiedSupplyRangeNodes = document
				.getElementsByName("qualifiedSupplyRange");
		var potentialSupplyRangeNodes = document
				.getElementsByName("potentialSupplyRange");
		var qualifiedSupplyRangeCheckedNodes = qualifiedSuppliersTree
				.getAllChecked();
		var qualifiedSupplyRangeCheckedArray = qualifiedSupplyRangeCheckedNodes
				.split(',');
		var potentialSupplyRangeCheckedNodes = potentialSuppliersTree
				.getAllChecked();
		var potentialSupplyRangeCheckedArray = potentialSupplyRangeCheckedNodes
				.split(',');
		if (potentialSupplyRangeCheckedNodes == ""
				&& qualifiedSupplyRangeCheckedNodes == "") {
			alert("供应商供货范围不能为空。");
			return;
		}
		var message = '';
		for ( var i = 0, length = providerNodes.length; i < length; i++) {
			var providerName = providerNodes[i].value;
			var qualifiedSupplyRange = eval("("
					+ (qualifiedSupplyRangeNodes[i].value == "" ? "[]"
							: qualifiedSupplyRangeNodes[i].value) + ")");
			var potentialSupplyRange = eval("("
					+ (potentialSupplyRangeNodes[i].value == "" ? "[]"
							: potentialSupplyRangeNodes[i].value) + ")");
			var sign = false;
			for ( var qcIndex in qualifiedSupplyRangeCheckedArray) {
				for ( var qIndex in qualifiedSupplyRange) {
					if (qualifiedSupplyRangeCheckedArray[qcIndex] == qualifiedSupplyRange[qIndex]) {
						sign = true;
						break;
					}
				}
				if (!sign) {
					var text = qualifiedSuppliersTree
							.getItemText(qualifiedSupplyRangeCheckedArray[qcIndex]);
					if (text != '0') {
						message += '\n';
						message += providerName;
						message += '       ';
						message += text;
					}
				}
				sign = false;
			}

			for ( var pcIndex in potentialSupplyRangeCheckedArray) {
				for ( var pIndex in potentialSupplyRange) {
					if (potentialSupplyRangeCheckedArray[pcIndex] == potentialSupplyRange[pIndex]) {
						sign = true;
						break;
					}
				}
				if (!sign) {
					var text = potentialSuppliersTree
							.getItemText(potentialSupplyRangeCheckedArray[pcIndex]);
					if (text != '0') {
						message += '\n';
						message += providerName;
						message += '       ';
						message += text;
					}
				}
				sign = false;
			}
		}

		if (message != '') {
			alert("以下供应商供货范围无法导出：" + message);
		}
		setUpFieldValue2(getUpFieldsByFieldval("导出制造部选择器").eq(0),
				"Account|" + companySelect.value);

		setUpFieldValue2(getUpFieldsByFieldval("导出制造部").eq(0),
				companySelect.options[companySelect.selectedIndex].text);

		setUpFieldValue2(getUpFieldsByFieldval("是否需总裁审批").eq(0), 1);

		writeProviderInfoToForm();
		if (getUpFieldValue(getUpFieldsByFieldval("单位选择器").eq(0)).id != "-8924145959034269856") {
			document.getElementsByName("groupManage")[0].checked = false;
			document.getElementsByName("groupManage")[1].checked = true;
		}
		writeQualifiedSupplyRangeToForm();
		writePotentialSupplyRangeToForm();

		setUpFieldValue2(getUpFieldsByFieldval("处理意见").eq(0), document
				.getElementsByName("opinion")[0].checked ? 1 : 2);
		setUpFieldValue2(getUpFieldsByFieldval("是否属于集团管控").eq(0), document
				.getElementsByName("groupManage")[0].checked ? 0 : 1);
		setUpFieldValue2(getUpFieldsByFieldval("集团管控").eq(0), document
				.getElementsByName("groupManage")[0].checked ? "在集团重点管控物料类别清单内"
				: "不在集团重点管控类别清单内");

		fy_dialog_close();
	} else {
		alert("需要选择导出制造部");
	}
}

// ////////////////////////////弹出窗口时，读取表单中填写的数据//////////////////
/**
 * 读取失效供应商信息
 */
function readProviderInfo() {
	var providerInfoMapping = {
		"所在组织" : 'organization',
		"失效公司名称" : 'companyName',
		"供应商编号" : 'companyCode',
		"失效地点" : 'companyAddress',
		"失效日期" : 'invalidDate',
		"供应商的合格供应商列表" : 'qualifiedSupplyRange',
		"供应商的潜在供应商列表" : 'potentialSupplyRange'
	};
	var length = getUpFieldsByFieldval("所在组织").length;
	if (length > 1) {
		var tr = document.getElementsByName("organization")[0].parentNode.parentNode;
		for ( var i = 1; i < length; i++) {
			addRepeatRow(tr);
		}
	}
	for ( var name in providerInfoMapping) {
		var fNodes = getUpFieldsByFieldval(name);
		var nodes = document.getElementsByName(providerInfoMapping[name]);
		for ( var i = 0; i < length; i++) {
			var value = getUpFieldsByFieldval(fNodes.eq(0)).value;
			nodes[i].value = value ?value :"";
		}
	}
}
/**
 * 设置失效范围节点勾选
 */
function setInvalidNodeChecked(tree, array) {
	var range0Array = getUpFieldsByFieldval(array[0]);
	var range1Array = getUpFieldsByFieldval(array[1]);
	var range2Array = getUpFieldsByFieldval(array[2]);

	var length = range0Array.length;
	for ( var i = 0; i < length; i++) {
		var id = null;
		var range2Value = getUpFieldValue(range2Array.eq(i)).value;
		if (range2Value != "") {
			id = range2Value;
		} else {
			var range1Value = getUpFieldValue(range1Array.eq(i)).value;
			if (range1Value != "") {
				id = range1Value;
			} else {
				var range0Value = getUpFieldValue(range0Array.eq(i)).value;
				if (range0Value != "") {
					id = range0Value;
				}
			}
		}
		if (id) {
			tree.setCheck(id, true);
		}
	}
};

/**
 * 读取失效范围
 */
function readInvalidRange() {
	setAllAllowCheckNodes();
	setInvalidNodeChecked(potentialSuppliersTree, [ "潜在供应商_失效范围1PK",
			"潜在供应商_失效范围2PK", "潜在供应商_失效范围3PK" ]);
	setInvalidNodeChecked(qualifiedSuppliersTree, [ "合格供应商_失效范围1PK",
			"合格供应商_失效范围2PK", "合格供应商_失效范围3PK" ]);

}
/**
 * 弹出窗口时，读取表单中填写的数据
 */
function readValueFromForm() {
	var opinionValue = getUpFieldValue(getUpFieldsByFieldval("处理意见").eq(0)).value;
	// 处理意见单选框
	document.getElementsByName("opinion")[0].checked = opinionValue == 0;
	document.getElementsByName("opinion")[1].checked = opinionValue == 1;

	readProviderInfo();

	readInvalidRange();
}

// ////////////设置表单输入框状态///////////////////////
function setFieldsState() {
	setFieldsCanFill("organization");
	setFieldsReadOnly("companyName");
	setFieldsCanFill("companyCode");
	setFieldsCanFill("companyAddress");
	setFieldsReadOnly("invalidDate");

	setFieldsCanFill("opinion");
}

/**
 * 导出的制造部选择
 */
function onCompanySelectChange() {
	clearAllRepeatTable();
	setTreeAllCheckBoxDisabled();
	if (getUpFieldsByFieldval("导出制造部选择器").length == 0
			|| getUpFieldValue(getUpFieldsByFieldval("导出制造部选择器").eq(0)).id == "") {
		setFieldsState();
	}
}

/**
 * 制造部
 */
function subsidiaryWrite() {
	var currentCorp = getUpFieldValue(getUpFieldsByFieldval("单位选择器").eq(0)).id;
	currentCorp = currentCorp.replace("Account|",'');
	document.getElementsByName("company")[0].value = currentCorp;
	if (currentCorp == "-8924145959034269856") {
		document.getElementById("groupManageTR").style.display = "";
	}

	setFieldsDisabled("company");
	setFieldsState();
	readValueFromForm();
}

/**
 * 总办填写
 */
function headquartersWrite() {
	setFieldsCanFill("company");

	document.getElementsByName("company")[0].value = getUpFieldValue(getUpFieldsByFieldval(
			"导出制造部选择器").eq(0)).id.replace("Account|","");

	if (document.getElementsByName("company")[0].value !== "") {
		setFieldsState();
		readValueFromForm();
	}
}
/**
 * 查询供应商名称
 */
function queryCompanyName(input_prividerCode) {
	if (document.getElementsByName("company")[0].value == "") {
		alert("导出制造部不能为空");
	} else {
		clearOtherInputValue(input_prividerCode.parentNode.parentNode);
		var code = input_prividerCode.value.replace(/ /g, '');
		input_prividerCode.value = code;

		_ajax(
				"./oa2uapController.do?method=findProviderInfoAndRangeByCode&code="
						+ code + "&accountID="
						+ document.getElementsByName("company")[0].value,
				function(text) {
					if (text == 1) {
						alert("无法获取对应的制造部ＩＤ");
					} else {
						var result = eval("(" + text + ")");

						if (result.state) {
							if (result.state == 1) {
								alert("不存在供应商编码：" + code);
							} else if (result.state == 2) {
								alert("该供应商在所选导出制造部无供货范围");
							} else if (result.state == 3) {
								setOtherInputValue(
										input_prividerCode.parentNode.parentNode,
										result);
								setAllAllowCheckNodes();
							} else {
								alert("数据异常，请重试");
							}
						} else {
							alert("获取数据异常");
						}
					}
				});
	}

	function setOtherInputValue(dom, result) {
		var childNodes = dom.childNodes;
		if (childNodes) {
			for ( var i = 0, length = childNodes.length; i < length; i++) {
				if (childNodes[i].tagName
						&& childNodes[i].tagName.toUpperCase() === 'INPUT') {
					if (childNodes[i].name === 'companyName'
							|| childNodes[i].name === 'qualifiedSupplyRange'
							|| childNodes[i].name === 'potentialSupplyRange') {
						childNodes[i].value = result[childNodes[i].name];
					}
				} else {
					setOtherInputValue(childNodes[i], result);
				}
			}
		}
	}

	function clearOtherInputValue(dom) {
		var childNodes = dom.childNodes;
		if (childNodes) {
			for ( var i = 0, length = childNodes.length; i < length; i++) {
				if (childNodes[i].tagName
						&& childNodes[i].tagName.toUpperCase() === 'INPUT') {
					if (childNodes[i].name === 'companyName'
							|| childNodes[i].name === 'qualifiedSupplyRange'
							|| childNodes[i].name === 'potentialSupplyRange') {
						childNodes[i].value = '';
					}
				} else {
					clearOtherInputValue(childNodes[i]);
				}
			}
		}
	}
}

window.onload = function() {
	createQualifiedSuppliersTree();
	createPotentialSuppliersTree();
	if (getUpFieldsByFieldval("总办填写表单_").length > 0) {
		headquartersWrite();
	} else {
		subsidiaryWrite();
	}
};
